package com.kaifamiao.controller;

import com.kaifamiao.preventrefresh.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KeyValueController {

    private final RedisService redisService;

    @Autowired
    public KeyValueController(RedisService redisService) {
        this.redisService = redisService;
    }

    @GetMapping("/get")
    public ResponseEntity<String> getValue(@RequestParam String key) {
        String value = redisService.get(key);
        return ResponseEntity.ok(value);
    }

    @PostMapping("/set")
    public ResponseEntity<String> setValue(@RequestParam String key, @RequestParam String value) {
        redisService.set(key, value);
        return ResponseEntity.ok("Value set successfully");
    }
    @PostMapping("/incr")
    public ResponseEntity<String> incr(@RequestParam String key) {
        Long value = redisService.incr(key);
        return ResponseEntity.ok(value.toString());
    }
}
